//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 10
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace CancelAnExistingMeeting
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // TODO: You must first have the calendar ItemId of a meeting you wish to cancel before calling
            // CancelAnExistingMeeting.
        }

        /// <summary>
        /// Cancels an exiting meeting on an organizers calendar (Listing 10-32)
        /// </summary>
        /// <param name="binding">Binding to use for the web service call</param>
        /// <param name="meetingCalendarItemId">Id of the meeting to cancel</param> 
        /// <param name="meetingCancellationBody">Optional message body to include
        /// in the resulting meeting cancellations</param>
        /// 
        public static void CancelAnExistingMeeting(
            ExchangeServiceBinding binding,
            ItemIdType meetingCalendarItemId,
            string meetingCancellationBody)
        {
            // Create the CancelCalendarItemType instance
            //
            CancelCalendarItemType cancelCalendarItem = new CancelCalendarItemType();
            cancelCalendarItem.ReferenceItemId = meetingCalendarItemId;

            // Set the NewBodyContent property if a meetingCancellationBody
            // was provided.
            //
            if (!String.IsNullOrEmpty(meetingCancellationBody))
            {
                cancelCalendarItem.NewBodyContent = new BodyType();
                cancelCalendarItem.NewBodyContent.BodyType1 = BodyTypeType.Text;
                cancelCalendarItem.NewBodyContent.Value =
                    "Sorry folks, my favorite tv series is back!";
            }

            // Assemble our CreateItem request
            //
            CreateItemType createItemRequest = new CreateItemType();
            createItemRequest.MessageDisposition = MessageDispositionType.SendOnly;
            createItemRequest.MessageDispositionSpecified = true;
            createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
            createItemRequest.Items.Items =
                new CancelCalendarItemType[] { cancelCalendarItem };

            // Call the CreateItem web method and check for success.
            //
            CreateItemResponseType response = binding.CreateItem(createItemRequest);
            ItemInfoResponseMessageType responseMessage =
                response.ResponseMessages.Items[0] as ItemInfoResponseMessageType;

            if (responseMessage.ResponseCode != ResponseCodeType.NoError)
            {
                throw new Exception(String.Format(
                    "Unable to cancel meeting\r\n{0}\r\n{1}",
                    responseMessage.ResponseCode,
                    responseMessage.MessageText));
            }
        }

    }
}
